package yxy.game.pm2.command.conquest;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.sdk.shengye.ShengYeSubscribeModule;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ConquestMessage;
import yxy.game.pm2.module.conquest.ConquestHandle;

/** 征战 挂机奖励收取 */
@MessageType(proto = Command.ConquestIdleBonus, classtype = ConquestMessage.ConquestIdleBonusRequest.class)
public class CMD_ConquestIdleBonus extends Command<ConquestMessage.ConquestIdleBonusRequest> {

	@Override
	public void execute(MessageSource messageSource, ConquestMessage.ConquestIdleBonusRequest request) {
		String playerCode = messageSource.getTag();

		ConquestMessage.ConquestIdleBonusResponse.Builder response = ConquestMessage.ConquestIdleBonusResponse.newBuilder();

		try {
			Result result = new ConquestHandle().idleBonus(playerCode, response);
			send(messageSource, response, result);
			
			if(result.getNumber() == Result.Success.getNumber()) {
				ShengYeSubscribeModule.updatenexttimes(playerCode);				
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, ConquestMessage.ConquestIdleBonusResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}